package com.mttk.api.addon.ai.pool.model;

import org.bson.Document;

import com.mttk.api.addon.AdapterConfig;
import com.mttk.api.util.StringUtil;

public abstract class AiModel {
	protected AdapterConfig config;

	public AiModel(AdapterConfig config) {
		this.config = config;
	}

	public AdapterConfig getConfig() {
		return config;
	}
	//得到prefix
	public  abstract String getPrefix();
	//如果模型自己处理参数则返回true
	public boolean applyProp(Document body,String key,String keyReal) {
		return false;
	}
	//
	public  String getModel() {
		//试图得到根据prefix得到url
		String key=getPrefix()+"_model";
		String url=config.getString(key);
		if(StringUtil.notEmpty(url)) {
			return url;
		}
		//
		throw new RuntimeException("Model  is not set from key:"+key);
	}
	//根据不同的模型类型获取url
	public  String getUrl() {
		//试图得到根据prefix得到url
		String key=getPrefix()+"_url";
		String url=config.getString(key);
		if(StringUtil.notEmpty(url)) {
			return url;
		}
		//
		throw new RuntimeException("Model URL is not set from key:"+key);
	}
	public void applyProps(Document body) {
		for(String key:config.keySet()) {
			String prefix="prop_"+getPrefix()+"_";
			if(!key.startsWith(prefix)) {
				continue;
			}
			//去掉prefix
			String keyReal=key.substring(prefix.length());
			//
			if(applyProp(body,key,keyReal)) {
				continue;
			}
			//
			Object value=config.get(key);
			if(value==null) {
				continue;
			}
			//
			body.append(keyReal, value);			
		}

	}
}
